<?php

namespace app\admin\controller\mk;
use app\admin\model\ShopSetting as ss;
use app\common\controller\Backend;

/**
 * 分享有礼
 */
class Share extends Backend
{
    /**
     * Share模型对象
     * @var object
     * @phpstan-var \app\admin\model\mk\Share
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id'];

    protected string|array $quickSearchField = ['id'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\mk\Share();
    }


    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */


    function setting(){
           $data = input("post.data");
           $act = input("post.act");
           switch ($act) {
               case 'post':
                   foreach($data as &$v){
                       if($v['status'] == 1){
                           if($v['value']=='' || $v['value']==0){
                               $this->success("ok",['code'=>0,'msg'=>$v['name'].'必填项']);
                           }
                       }
                   }
                   $find = ss::where(['type'=>'share','cs'=>0])->find();
                   if($find){
                         ss::where(['type'=>'share','cs'=>0])->update(['settings'=>json_encode($data)]);
                   }else{
                         ss::insert([
                                'type'=>'share',
                                'settings'=>json_encode($data)
                         ]);
                   }
                   $this->success("ok",['code'=>1,'msg'=>'成功']);
                   break;
               
               default:
                   $find = ss::where(['type'=>'share','cs'=>0])->find();
                   $data = json_decode($find['settings'],true);
                   foreach($data as &$v){
                        if($v['name']=='优惠券'){
                             $v['value'] = intval($v['value']);
                        }
                   }
                   $this->success("ok",['code'=>1,'data'=>$data]);
                   break;
           }
           
            
    }
}